module scene {

    export class GameOverScene extends eui.Component {
        constructor(score, level) {
            super();
            this.level = level
            this.score = score
            

            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "GameOverPageSkin";
        }
        private rankPage: page.RankPage
        private MaxScore: number = 0
        private level: number
        private score: number
        private overLevelText: eui.Label
        private overScoreText: eui.Label
        private onRank: eui.Label
        private overShare: eui.Image
        private overAgain: eui.Image
        private SmallPage: page.GameFinishSmallPage

        private onComplete() {
            manager.StatisticsManager.sceneCount(1008)

            this.rankPage = new page.RankPage();
            this.addChild(this.rankPage);
            this.rankPage.visible = false
            // let userSetting: user.UserSetting = manager.GameManager.getInstance().getUserSetting();

            let userSetting = new user.UserSetting()

            if (userSetting.maxScore < this.score) {
                console.log("userSetting.maxScore < this.score" + this.score)

                this.submitWorldRankScore(this.score);
                
                // this.submitScore(this.score)
                let data = {
                    "key": "score",
                    "value": this.score.toString(),
                };
                let scoreData = [data];
                this.rankPage.commit(page.RankPage.SUBMIT, scoreData);
                userSetting.saveMaxScore(this.score);
                
                
            }





            this.SmallPage = new page.GameFinishSmallPage()
            this.addChild(this.SmallPage)
            this.SmallPage.commit(this.score);
            this.SmallPage.show();

            this.overLevelText.text = `${this.level}`
            this.overScoreText.text = `${this.score}`

            this.onRank.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showRank, this)
            this.overAgain.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAgain, this)
            this.overShare.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onShare, this)
        }   
        
        private showRank() {
            this.onAgain()

            let event: events.SceneEvent = new events.SceneEvent(game.SceneIds.RANK_SCENE_ID, {}, events.SceneEvent.POPUP_SHOW_TYPE);
            facade.GameFacade.getInstance().dispatchEvent(event);
        }

        private onAgain() {
            manager.StatisticsManager.click(157)
            this.SmallPage.hide()
            this.SmallPage.stop()
            this.removeChild(this.SmallPage)
            let event2: events.GameAgainEvent = new events.GameAgainEvent()
            this.dispatchEvent(event2)
            let event: events.SceneEvent = new events.SceneEvent(game.SceneIds.START_SCENE_ID, {});
            facade.GameFacade.getInstance().dispatchEvent(event);
            wx.setStorage({
                key: 'relive',
                data:{
                    score: 0,
                    level: 0
                }
            })


            
        }

        private onShare() {
            manager.StatisticsManager.click(158)
            manager.GameManager.getInstance().commonShare();
        }


        private submitWorldRankScore(score: number): void {
            let postData: SubmitScorePostData = new SubmitScorePostData();
            postData.token = manager.GameManager.getInstance().getStatisticManager().appToken;
            postData.score = score;
            postData.other = 1;
            let request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            let url: string = manager.StatisticsManager.domain
                + '/index.php?g=WxaGame&m=GameRank&a=update_rank&session_3rd=' + manager.GameManager.getInstance().session_3rd;
            request.open(url, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send(postData);
            request.addEventListener(egret.Event.COMPLETE, this.onSubmitScoreComplete, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSubmitScoreError, this);
        }

        private onSubmitScoreComplete(event: egret.Event): void {
            egret.log("提交世界排行分数完成");
            let request = <egret.HttpRequest>event.currentTarget;
            console.log(JSON.parse(request.response));
        }

        private onSubmitScoreError(event: egret.Event): void {
            egret.log("提交世界排行分数失败");
            let request = <egret.HttpRequest>event.currentTarget;
            console.log(JSON.parse(request.response));
        }
    }

    export class SubmitScorePostData {
        public token: string = "";
        public score: number = 0;
        public other: number = 1;
    }
}